var express = require('express');
var router = express.Router();
const user = require('../utils/model/user')
const {deviceObj} = require('../utils/model/device_info')
const {successData, errorData} = require("../utils/response");

router.get('/userList', function (req, res, next) {
  user.findAll().then(data => {
    return successData(res, data)
  }).catch(err => {
    return errorData(res, err)
  })
});

router.get('/deviceList', function (req, res, next) {
  let {pageNum, pageSize, user_id, platform, uuid, ip_info, use_count, dateTime,} = req.query
  if (!pageSize) pageSize = 10
  if (!pageNum) pageNum = 0
  deviceObj.getAllDevices({
    user_id, platform, uuid, ip_info, use_count, dateTime,
    offset: Number(pageNum), limit: Number(pageSize)
  }).then(data => {
    return successData(res, data)
  })
});

router.get('/deviceInfo', function (req, res, next) {
  let uuid = req.query.uuid
  if (!uuid) return res.json({msg: "缺少参数uuid！"})
  deviceObj.getDeviceInfo({uuid}).then(data => {
    return successData(res, data)
  })
});

router.get('/updateDevice', function (req, res, next) {
  let uuid = req.query.uuid
  if (!uuid) return res.json({msg: "缺少参数uuid！"})
  deviceObj.updateDevice({...req.query, uuid}).then(data => {
    if (data && data.length) return successData(res, "修改成功！")
    return errorData(res, "error")
  })
});

module.exports = router;
